<?php
/**
 * StartController系统初始化
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: StartController.php 728 2011-08-09 01:58:26Z maomao.thx@gmail.com $
 */
class StartController extends IG_Controller_Action
{
    private $_cacheVersion = '001';

    private $_cache;

    public function indexAction()
    {
        set_time_limit(0);
        $this->disableLayout();
        $this->setNoRender();
        $frontendOptions = array(
            'lifetime' => 12 * 7200 ,  // cache lifetime of 2 hours
            'automatic_serialization' => true
        );

        $this->_cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions);

        $this->_cacheActiveAccount();
    }

    /**
     * 将活动系统的账号ID缓存在内存中
     */
    private function _cacheActiveAccount()
    {

        try {
          $db = Zend_Db_Table::getDefaultAdapter();

          $count = $db->query('select count(*) as c from  ad_active_participant')
              ->fetchAll();
          $count  = $count[0]['c'];

          $per = 100000;
          $times = (int)$count / $per + 1;
          for($i = 1; $i <= $times; $i++) {
            $sql = "select accountid from ad_active_participant limit {$per} offset " . ($i- 1) * $per;
            $data = $db->query($sql)
                ->fetchAll();
            foreach ($data as $row) {
                $key = 'system_cache' . $this->_cacheVersion. '_active_account_' . $row['accountid'];
                $this->_cache->save(1, $key);
            }
            echo '缓存' . ($i-1) * $per . '条<br />';
          }
          echo '活动之后缓存完毕, 共 ' . $count  . ' 条！';
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }
}